package com.ecommerce.repository;

import com.ecommerce.entity.Product;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {
    
    // 根据分类查找商品
    Page<Product> findByCategoryId(Long categoryId, Pageable pageable);
    
    // 根据状态查找商品
    Page<Product> findByStatus(Product.ProductStatus status, Pageable pageable);
    
    // 根据品牌查找商品
    Page<Product> findByBrand(String brand, Pageable pageable);
    
    // 根据关键词搜索商品
    @Query("SELECT p FROM Product p WHERE p.name LIKE %:keyword% OR p.description LIKE %:keyword%")
    Page<Product> searchByKeyword(@Param("keyword") String keyword, Pageable pageable);
    
    // 查找热门商品（按浏览量排序）
    @Query("SELECT p FROM Product p ORDER BY p.viewCount DESC")
    List<Product> findHotProducts(Pageable pageable);
    
    // 查找最新商品（按创建时间排序）
    @Query("SELECT p FROM Product p ORDER BY p.createdAt DESC")
    List<Product> findNewProducts(Pageable pageable);
    
    // 查找推荐商品（按销量排序）
    @Query("SELECT p FROM Product p ORDER BY p.soldCount DESC")
    List<Product> findRecommendedProducts(Pageable pageable);
    
    // 根据价格范围查找商品
    @Query("SELECT p FROM Product p WHERE p.price BETWEEN :minPrice AND :maxPrice")
    Page<Product> findByPriceRange(@Param("minPrice") Double minPrice, @Param("maxPrice") Double maxPrice, Pageable pageable);
} 